package com.android.common.mvp.view.fragment;

import android.content.ContentValues;
import android.view.View;
import android.widget.Toast;
import androidx.constraintlayout.utils.widget.ImageFilterView;
import androidx.databinding.ViewDataBinding;
import com.android.common.R;
import com.android.common.base.BaseFragment;
import com.android.common.constant.Constant;
import com.android.common.db.Columns;
import com.android.common.db.EnvDBHelper;
import com.google.android.material.textfield.TextInputEditText;
import java.util.Objects;

/**
 * 切换环境
 */
public class SwitchEnvFragment extends BaseFragment<ViewDataBinding> {
    //服务端接口地址
    TextInputEditText mEtServerApiAddress;
    //H5地址
    TextInputEditText mEtH5Address;
    //其它地址
    TextInputEditText mEtOtherAddress;

    @Override
    protected int onSetRootViewId() {
        return R.layout.fragment_switch_env;
    }

    @Override
    protected void onFindView() {
        super.onFindView();
        mEtServerApiAddress = mRootView.findViewById(R.id.et_server_api_address);
        mEtH5Address = mRootView.findViewById(R.id.et_h5_address);
        mEtOtherAddress = mRootView.findViewById(R.id.et_other_address);
    }

    @Override
    protected void onInitView() {
        super.onInitView();
        mEtServerApiAddress.setText(Constant.getBaseUrl());
        mEtH5Address.setText(Constant.getH5Url());
        mEtOtherAddress.setText(Constant.getOtherUrl());
    }

    protected String onSetTitleText() {
        return getString(R.string.switch_env);
    }

    @Override
    protected int onSetToolbarRightViewId() {
        return R.layout.common_ifv;
    }

    @Override
    protected void onInitToolbarRightViews(View views) {
        ImageFilterView ifvCommit = views.findViewById(R.id.ifv_commit);
        ifvCommit.setOnClickListener(view -> {
            ContentValues values = new ContentValues();
            values.put(Columns.ENV_SERVER_API_ADDRESS,mEtServerApiAddress.getText().toString());
            values.put(Columns.ENV_H5_ADDRESS,mEtH5Address.getText().toString());
            values.put(Columns.ENV_OTHER_ADDRESS,mEtOtherAddress.getText().toString());
            EnvDBHelper.getInstance(Constant.GLOBAL_APPLICATION).update(values);
            Toast.makeText(mActivity, R.string.operation_success, Toast.LENGTH_SHORT).show();
            requireActivity().finish();
        });
    }
}